/* 
 * Shuriken is the legal property of its developers. Please refer to the
 * COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Text;

namespace Shuriken.Core.Store
{
    internal class ApplicationCommandHelper
    {
        List<string> m_WindowClassNames;
        /// <summary>
        /// Application command helper class 
        /// </summary>
        /// <param name="WindowClass">name of the window class</param>
        public ApplicationCommandHelper(string WindowClassName)
        {
            m_WindowClassNames = new List<string>();
            m_WindowClassNames.Add(WindowClassName);
        }

        /// <summary>
        /// Application command helper class 
        /// </summary>
        /// <param name="WindowClasses">list of the names of the window classes</param>
        public ApplicationCommandHelper(List<string> WindowClassNames)
        {
            m_WindowClassNames = new List<string>();
            m_WindowClassNames.AddRange(WindowClassNames);
        }

        /// <summary>
        /// Retrieves the handle for a Window. 
        /// </summary>
        /// <returns>Window Handle</returns>
        private System.IntPtr GetWindowHandle()
        {
            System.IntPtr hwndWindow = System.IntPtr.Zero;

            foreach (string wndClass in m_WindowClassNames)
            {
                hwndWindow = Shell.ShellAPI.FindWindow(wndClass, null);

                if (System.IntPtr.Zero != hwndWindow) break;
            }

            return hwndWindow;
        }

        /// <summary>
        /// Sends an app command to the specified Window
        /// </summary>
        /// <param name="cmd">Application Command</param>
        public void SendAppCommand(int cmd)
        {
            System.IntPtr hwndWindow = GetWindowHandle();
            
            if (System.IntPtr.Zero != hwndWindow)
            {
                Shell.ShellAPI.SendMessage(hwndWindow, Shell.ShellAPI.WM_APPCOMMAND, 0, (cmd << 16));
            }
        }

        /// <summary>
        /// Play Pause Command 
        /// </summary>
        public void PlayPauseCommand()
        {
            SendAppCommand(Shell.ShellAPI.APPCOMMAND_MEDIA_PLAY_PAUSE);
        }

        /// <summary>
        /// Mute Volume Command 
        /// </summary>
        public void VolumeMuteCommmand()
        {
            SendAppCommand(Shell.ShellAPI.APPCOMMAND_VOLUME_MUTE);
        }

        /// <summary>
        /// Volume Up Command 
        /// </summary>
        public void VolumeUpCommand()
        {
            SendAppCommand(Shell.ShellAPI.APPCOMMAND_VOLUME_UP);
            SendAppCommand(Shell.ShellAPI.APPCOMMAND_VOLUME_UP);
            SendAppCommand(Shell.ShellAPI.APPCOMMAND_VOLUME_UP);
            SendAppCommand(Shell.ShellAPI.APPCOMMAND_VOLUME_UP);
            SendAppCommand(Shell.ShellAPI.APPCOMMAND_VOLUME_UP);
        }

        /// <summary>
        /// Volume Down Command 
        /// </summary>
        public void VolumeDownCommand()
        {
            SendAppCommand(Shell.ShellAPI.APPCOMMAND_VOLUME_DOWN);
            SendAppCommand(Shell.ShellAPI.APPCOMMAND_VOLUME_DOWN);
            SendAppCommand(Shell.ShellAPI.APPCOMMAND_VOLUME_DOWN);
            SendAppCommand(Shell.ShellAPI.APPCOMMAND_VOLUME_DOWN);
            SendAppCommand(Shell.ShellAPI.APPCOMMAND_VOLUME_DOWN);
        }

        /// <summary>
        /// Next Track Command 
        /// </summary>
        public void NextTrackCommand()
        {
            SendAppCommand(Shell.ShellAPI.APPCOMMAND_MEDIA_NEXTTRACK);
        }

        /// <summary>
        /// Previous Track Command 
        /// </summary>
        public void PreviousTrackCommand()
        {
            SendAppCommand(Shell.ShellAPI.APPCOMMAND_MEDIA_PREVIOUSTRACK);
        }

    }
}
